
#include "gl_renderer.h"
#include <GL/gl.h>
#include <GL/glu.h>

gl_renderer::gl_renderer(void)
{
	LOG("hello gl_renderer ......\n");

}

gl_renderer::~gl_renderer(void)
{

}

void gl_renderer::MessageImmediate(int message_id, void *param)
{
	switch(message_id)
	{
	case MSG_I_WINDOW_RESIZE:
	{
		WINDOW_RESIZE_PARAM *temp = (WINDOW_RESIZE_PARAM *)param;
		ResetViewport(temp->width, temp->height);
	}
	break;
	}

	v3d_renderer::MessageImmediate(message_id,param);
}

struct Vertex
{
    float tu, tv;
    float x, y, z;
};

Vertex g_quadVertices[] =
{
    { 0.0f,0.0f, -1.0f,-1.0f, 0.0f },
    { 1.0f,0.0f,  1.0f,-1.0f, 0.0f },
    { 1.0f,1.0f,  1.0f, 1.0f, 0.0f },
    { 0.0f,1.0f, -1.0f, 1.0f, 0.0f }
};

void gl_renderer::Update(int message_id, void *param)
{
	switch(message_id)
	{
	case MSG_I_RENDERUPDATE:
	{
		//begin rendering
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();
		glTranslatef( 0.0f, 0.0f, -3.0f );

		glEnable(GL_TEXTURE_2D);

		glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
		glDrawArrays( GL_QUADS, 0, 4 );


		glFlush();


	}
	break;

	}
	v3d_renderer::Update(message_id,param);
}

void gl_renderer::InitRenderer()
{


}

void gl_renderer::ResetViewport(int width, int height)
{
	LOG("%d %d \n ", width, height);
	glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  /* modify this line to change perspective values */
    gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}




